package com.fanjf.swipebacktemplate;

import android.annotation.TargetApi;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RelativeLayout;

import com.fanjf.swipebacktemplate.system.SystemBarTintManager;

/**
 *
 * Created by fanjf on 2016/11/22.
 */

public abstract class SwipeBackBaseActivity extends SwipeBackActivity {

    protected String TAG = this.getClass ().getSimpleName();

    private NetWorkBroadCast netWorkBroadCast;

    protected RelativeLayout mNetWorkWarningLayout;

    /**状态栏是否开启沉浸模式，set方法在oncreate前执行 */
    private boolean statusBarTintEnabled = true;

    @Override
    protected void onCreate ( Bundle savedInstanceState ) {
        super.onCreate ( savedInstanceState );
        netWorkBroadCast = new NetWorkBroadCast();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction( ConnectivityManager.CONNECTIVITY_ACTION);
        registerReceiver(netWorkBroadCast,intentFilter);
    }

    protected abstract void findView();

    /* 如果系统版本大于4.4则启用沉浸模式状态栏
    * @see android.app.Activity#setContentView(int)
    */
    @TargetApi (Build.VERSION_CODES.ICE_CREAM_SANDWICH) @Override
    public void setContentView(int layoutResID) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP  && isStatusBarTintEnabled()) {
            View view = LayoutInflater.from(this).inflate(layoutResID, null);
            view.setFitsSystemWindows(true);
            ((ViewGroup) view).setClipToPadding(true);
            super.setContentView(view);
        }else {
            super.setContentView(layoutResID);
        }
    }

    /* 如果系统版本大于4.4则启用沉浸模式状态栏
    * @see android.app.Activity#setContentView(int)
    */
    @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) @Override
    public void setContentView(View view) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP  && isStatusBarTintEnabled()) {
            view.setFitsSystemWindows(true);
            ((ViewGroup) view).setClipToPadding(true);
            super.setContentView(view);
        }else {
            super.setContentView(view);
        }
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        onPostCreateColor(((BaseApplication)getApplication()).getBackgroundColor());
    }
    protected void onPostCreateColor(int resId) {
        //如果系统版本大于4.4则启用沉浸模式状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP && isStatusBarTintEnabled()) {
            setTranslucentStatus(true);
            SystemBarTintManager tintManager = new SystemBarTintManager(this);
            tintManager.setStatusBarTintEnabled(true);
            tintManager.setStatusBarTintResource(resId);
            tintManager.setNavigationBarTintEnabled(true);
        }
    }

    /**
     * 是否开启沉浸模式
     */
    public boolean isStatusBarTintEnabled() {
        return statusBarTintEnabled;
    }

    public void setStatusBarTintEnabled(boolean statusBarTintEnabled) {
        this.statusBarTintEnabled = statusBarTintEnabled;
    }

    @TargetApi(19)
    private void setTranslucentStatus(boolean on) {
        Window win = getWindow();
        WindowManager.LayoutParams winParams = win.getAttributes();
        final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
        if (on) {
            winParams.flags |= bits;
        } else {
            winParams.flags &= ~bits;
        }
        win.setAttributes(winParams);
    }

    /**广播监听网络是否正常**/
    public class NetWorkBroadCast extends BroadcastReceiver {
        @Override
        public void onReceive( Context context, Intent intent) {
            if(mNetWorkWarningLayout == null){
                return;
            }
            if(NetUtil.isNetworkConnected(context)){
                mNetWorkWarningLayout.setVisibility( View.GONE);
            }else{
                mNetWorkWarningLayout.setVisibility(View.VISIBLE);
            }
        }
    }
}
